package approval

import (
	"encoding/json"
	"strconv"
	"oxy_server/model"
	"oxy_server/module/log"
	"oxy_server/until"
	"time"

	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
)

type (
	Approval struct {
		ID               bson.ObjectId `json:"_id" bson:"_id"`
		Productid        string        `json:"productid" bson:"productid"`               //产品id
		Applicant        string        `json:"applicant" bson:"applicant"`               //申请人
		Approver         string        `json:"approver" bson:"approver"`                 //审批人
		OldState         string        `json:"oldstate" bson:"oldstate"`                 //原状态
		CurrentState     string        `json:"currentstate" bson:"currentstate"`         //当前状态
		AimState         string        `json:"aimstate" bson:"aimstate"`                 //目标状态
		ApplicationInfo  string        `json:"applicationinfo" bson:"applicationinfo"`   //申请意见
		ApproverInfo     string        `json:"approverinfo" bson:"approverinfo"`         //审批意见
		Largecargonumber string        `json:"largecargonumber" bson:"largecargonumber"` //大货号
		Result           string        `json:"result" bson:"result"`                     //大货号
		Applicationtime  time.Time     `json:"applicationtime" bson:"applicationtime"`   //申请时间
		ApproverTime     time.Time     `json:"approvertime" bson:"approvertime"`         //审批时间

	}
	insertApproval struct {
		Productid        string    `json:"productid" bson:"productid"`               //产品id
		Applicant        string    `json:"applicant" bson:"applicant"`               //申请人
		Approver         string    `json:"approver" bson:"approver"`                 //审批人
		OldState         string    `json:"oldstate" bson:"oldstate"`                 //原状态
		CurrentState     string    `json:"currentstate" bson:"currentstate"`         //当前状态
		AimState         string    `json:"aimstate" bson:"aimstate"`                 //目标状态
		ApplicationInfo  string    `json:"applicationinfo" bson:"applicationinfo"`   //申请意见
		ApproverInfo     string    `json:"approverinfo" bson:"approverinfo"`         //审批意见
		Largecargonumber string    `json:"largecargonumber" bson:"largecargonumber"` //大货号
		Result           string    `json:"result" bson:"result"`                     //大货号
		Applicationtime  time.Time `json:"applicationtime" bson:"applicationtime"`   //申请时间
		ApproverTime     time.Time `json:"approvertime" bson:"approvertime"`         //审批时间

	}
)

type OXYApprovalData struct{}

func NewOXYDesignPlanData() *OXYApprovalData {
	designPlanData := &OXYApprovalData{}
	return designPlanData
}

func (opd *OXYApprovalData) GetListHandler(c echo.Context) error {
	log.Info("GetListHandler")
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "approval"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	query, count, err := model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []Approval{}
	query.All(&ps)
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  ps,
		"count": count,
	})

	return nil
}

func (opd *OXYApprovalData) GetInfoHandler(c echo.Context) error {
	log.Info("GetDesignPlanInfoHandler")
	id := c.QueryParam("id")
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "approval"
	query, err := model.DB().QueryOne(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p := Approval{}
	query.One(&p)
	str, err := json.Marshal(p)
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
		"data": string(str),
	})

	return nil
}

func (opd *OXYApprovalData) AddHandler(c echo.Context) error {
	log.Info("AddHandler")
	o := &insertApproval{}
	if err := c.Bind(o); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyCms"
	model.DB().Document = "approval"
	err := model.DB().Insert(o)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "product_plan_order"
	err = model.DB().Update(bson.M{"_id": bson.ObjectIdHex(o.Productid)}, bson.M{"$set": bson.M{"state": o.CurrentState}})
	if err != nil {
		log.Error("更新产品列表状态失败", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})
	return nil
}

func (opd *OXYApprovalData) UpdateHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateDesignPlanHandler", id)

	o := &insertApproval{}
	if err := c.Bind(o); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyCms"
	model.DB().Document = "approval"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, o)
	if err != nil {
		log.Error("更新出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyCms"
	model.DB().Document = "product_plan_order"
	data := bson.M{}
	if o.Result == "ok" {
		data = bson.M{"$set": bson.M{"state": o.AimState}}
	} else if o.Result == "ng" {
		data = bson.M{"$set": bson.M{"state": o.OldState}}
	} else {
		data = bson.M{"$set": bson.M{"state": o.CurrentState}}
	}
	err = model.DB().Update(bson.M{"_id": bson.ObjectIdHex(o.Productid)}, data)
	if err != nil {
		log.Error("更新出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})
	return nil
}

func (opd *OXYApprovalData) DelHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelDesignPlanHandler", id)
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "approval"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错 approval", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Document = "design_property"
	err = model.DB().Remove(bson.M{"designplanid": id})
	if err != nil {
		log.Error("删除出错 design_property", err)
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
